﻿using UnityEngine;
using Devil;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace GameToolkit.BehaviourTree
{
    /// <summary>
    /// 行为树资源
    /// </summary>
    [System.Serializable, JsonImporter(FormerlySerializedAs = "BehaviourTreeAsset")]
    sealed public class BehaviourTreeAsset : ScriptableObject, IThreadSafeTitle
    {
        string _nameCopy;
        /// <summary>
        /// 标题（同 name），由于 name 不允许多线程访问，所以提供该属性用于多线程访问。
        /// </summary>
        public string Title
        {
            get
            {
                return _nameCopy;
            }
        }

#if UNITY_EDITOR
        [System.Serializable]
        internal struct Group
        {
            public string title;

            [SerializeReference]
            public Composite[] composites;
        }

        [SerializeField, HideInInspector]
        internal Group[] m_Groups;

        [SerializeField, HideInInspector]
        internal bool m_ShowInspector = true;
#endif

        [SerializeField, HideInInspector]
        internal Vector2 m_RootPosition;

        [SerializeField, HideInInspector]
        internal int m_RootIndex;

        [SerializeReference, HideInInspector]
        internal Composite[] m_Nodes;

        //[SerializeField]
        //internal Transition[] m_Transitions;

        private void Awake()
        {
            _nameCopy = name;
        }

        public void UpdateTitle()
        {
            _nameCopy = name;
        }

#if UNITY_EDITOR

        private void OnValidate()
        {
            _nameCopy = name;
        }
#endif
    }

}
